unit UCadSubArea;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Modelo, StdCtrls, Mask, DBCtrls, DB, DBTables, Grids, DBGrids,
  Buttons, CheckLst, ExtCtrls, UDPTOUteis;

type
  TFCadSubArea = class(TFModelo)
    qPadraoSUBARE_DESCRICAO: TStringField;
    qPadraoSUBARE_CODIGO: TIntegerField;
    qPadraoARE_CODIGO: TIntegerField;
    lbl1: TLabel;
    dbedt1: TDBEdit;
    lbl2: TLabel;
    dbedt2: TDBEdit;
    lbl3: TLabel;
    dbedt3: TDBEdit;
    EdDescArea: TEdit;
    procedure dbedt3DblClick(Sender: TObject);
    procedure dbedt3Exit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadSubArea: TFCadSubArea;

implementation

uses UPesqArea;

{$R *.dfm}

procedure TFCadSubArea.dbedt3DblClick(Sender: TObject);
begin
  inherited;
  dbedt3.Text := DPTOChamaPesquisa(FPesqArea,TFPesqArea,'ARE_CODIGO', QPadrao, dbedt3.Text);
end;

procedure TFCadSubArea.dbedt3Exit(Sender: TObject);
begin
  inherited;
   if dbedt3.Text <> '' then
   begin
        if ((qPadrao.state <> dsEdit) and (qPadrao.State <> dsInsert)) then
             qPadrao.Edit;

        qGenerica.Close;
        qGenerica.SQL.Clear;

        qGenerica.SQL.Add('Select ARE_DESCRICAO FROM AREA WHERE ARE_CODIGO = ' + QuotedStr(dbedt3.Text));
        qGenerica.Open;

        EdDescArea.Text := qGenerica.fieldByName('ARE_DESCRICAO').AsString;


        // CALCULA O CODIGO DA SUBAREA JA COM BASE NA AREA
        qGenerica.Close;
        qGenerica.SQL.Clear;

        qGenerica.SQL.Add('Select MAX(SUBARE_CODIGO) as COD_SUBAREA FROM SUB_AREA WHERE ARE_CODIGO = ' + QuotedStr(dbedt3.Text));
        qGenerica.Open;

        qPadrao.FieldByName('SUBARE_CODIGO').AsInteger := qGenerica.fieldByName('COD_SUBAREA').AsInteger + 1;

        dbedt1.SetFocus;
        
        qGenerica.Close;
        qGenerica.Free;


   end;
end;

end.
